package com.moshang.blog.controller.back.quartz;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.moshang.blog.core.annotation.SystemLog;
import com.moshang.blog.core.utils.PageUtil;
import com.moshang.blog.core.utils.RestResponse;
import com.moshang.blog.entity.ScheduleJob;
import com.moshang.blog.quartz.service.QuartzService;
import com.moshang.blog.service.ScheduleJobService;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.util.WebUtils;

import javax.servlet.ServletRequest;
import java.util.Date;
import java.util.Map;

/**
 * @author 定时任务
 * Created on 2019/4/25.
 */
@Controller
@RequestMapping("/back/quartz")
public class QuartzController {
    @Autowired
    private ScheduleJobService scheduleJobService;
    @Autowired
    private QuartzService quartzService;
    @RequestMapping("list")
    public String showList(){

        return "back/quartz/list";
    }
    @PostMapping("list")
    @ApiOperation(value = "/list", httpMethod = "POST", notes = "查询定时任务列表")
    @ResponseBody
    public PageUtil<ScheduleJob> list(@RequestParam(value = "page",defaultValue = "1")Integer page,
                                      @RequestParam(value = "limit",defaultValue = "10")Integer limit,
                                      ServletRequest request){
        Map map = WebUtils.getParametersStartingWith(request, "s_");
        PageUtil<ScheduleJob> pageUtil = new PageUtil<>();
        QueryWrapper<ScheduleJob> wrapper = new QueryWrapper<>();
        if(!map.isEmpty()){
            String name = (String) map.get("name");
            if(StringUtils.isNotBlank(name)) {
                wrapper.eq("job_name", name);
            }
        }
        wrapper.eq("delete_flag",0);
        wrapper.orderByDesc("createTime");
        IPage<ScheduleJob> dataPage = scheduleJobService.page(new Page<>(page,limit),wrapper);
        pageUtil.setCount((int) dataPage.getTotal());
        pageUtil.setData(dataPage.getRecords());
        return pageUtil;
    }
    /**
     * 进入添加页面
     */
    @RequestMapping("addUI")
    public String addUI(Model model){
        return "back/quartz/add";
    }

    /**
     * 添加
     */
    @PostMapping("add")
    @SystemLog("新增定时任务")
    @ApiOperation(value = "/add", httpMethod = "POST", notes = "新增定时任务")
    @ResponseBody
    public RestResponse add(ScheduleJob scheduleJob){
        String type="insert";
        if(StringUtils.isBlank(scheduleJob.getJobName())){
            return RestResponse.failure("名称不能为空");
        }
        scheduleJobService.saveOrUpdateDict(scheduleJob);
        return RestResponse.success();
    }
    /**
     * 进入修改页面
     */
    @RequestMapping("editUI")
    public String editUI(@RequestParam("id") Integer id,Model model){
        ScheduleJob scheduleJob=scheduleJobService.getById(id);
        model.addAttribute("scheduleJob",scheduleJob);
        return "back/quartz/edit";
    }

    /**
     * 删除(逻辑删除)
     */
    @PostMapping("delete")
    @SystemLog("逻辑删除任务")
    @ApiOperation(value = "/delete", httpMethod = "POST", notes = "逻辑删除任务")
    @ResponseBody
    public RestResponse deleteById(@RequestParam("id") Integer id){
        ScheduleJob scheduleJob=scheduleJobService.getById(id);
        scheduleJob.setDeleteFlag(true);
        scheduleJobService.updateById(scheduleJob);
        quartzService.remove(scheduleJob);
        return RestResponse.success();
    }


    @PostMapping("updateState")
    @SystemLog("修改任务状态")
    @ResponseBody
    @ApiOperation(value = "/updateState", httpMethod = "POST", notes = "修改任务状态")
    public  RestResponse updateState(@RequestParam("id") Integer id,@RequestParam("status") Integer status){
        if(id==null){
            return RestResponse.failure("id不能为空");
        }
       ScheduleJob scheduleJob=scheduleJobService.getById(id);
        scheduleJob.setStatus(status);
        scheduleJob.setUpdateTime(new Date());
        scheduleJobService.updateById(scheduleJob);
        if (status==1){
            quartzService.startJob(scheduleJob);
            return RestResponse.success("任务已启动");
        }else {
            quartzService.remove(scheduleJob);
            return RestResponse.success("任务暂停");

        }
    }
}
